//
//  NewCoDictionary.swift
//  AdapterPattern
//
//  Created by LY on 2017/9/29.
//  Copyright © 2017年 DesignPatterns. All rights reserved.
//

import Foundation
//新公司数据
class NewCoStaffMember {
    private var name : String
    private var role : String
    init(name:String,role:String) {
        self.name = name
        self.role = role
    }
    func getName() -> String {
        return name
    }
    func getRole()->String{
        return role
    }
}
class NewCoDictionary {
    private var staff:[String:NewCoStaffMember]
    init() {
        staff = ["Hans":NewCoStaffMember(name: "Hans", role: "Corp Counsel"),"Greta":NewCoStaffMember(name: "Greta", role: "VP,Legal")]
    }
    func getStaff() -> [String:NewCoStaffMember] {
        return staff
    }
}
//合并数据类型
//extension NewCoDictionary : EmployeeDataSource{
//    var employees: [Employee] {
//        return self.staff.map({ (name,value) -> Employee in
//            return Employee(name: value.getName(), title: value.getRole())
//        })
//    }
//    private func createEmployees(filterClosure:((NewCoStaffMember) -> Bool)) -> [Employee]{
//        return self.staff.map({ (name,item) -> Employee in
//            return Employee(name: item.getName(), title: item.getRole())
//        })
//    }
//    func searchByName(name: String) -> [Employee] {
//        return createEmployees(filterClosure: { (item) -> Bool in
//            return item.getName().rangeOfCharacter(from: CharacterSet(charactersIn: name)) != nil
//        })
//    }
//
//    func searchByTitle(title: String) -> [Employee] {
//        return createEmployees(filterClosure: { (item) -> Bool in
//            return item.getRole().rangeOfCharacter(from: CharacterSet(charactersIn: title)) != nil
//        })
//    }
//}

class NewCoDictionaryAdapter: EmployeeDataSource {
    func searchByName(name: String) -> [Employee] {
        return createEmployess(filterColure: { (item) -> Bool in
            return item.name.rangeOfCharacter(from: CharacterSet(charactersIn: name)) != nil
        })
    }
    
    func searchByTitle(title: String) -> [Employee] {
        return createEmployess(filterColure: { (item) -> Bool in
            return item.title.rangeOfCharacter(from: CharacterSet(charactersIn: title)) != nil
        })
    }
    
    private let directory:NewCoDictionary
    init() {
        directory = NewCoDictionary()
    }
    var employees: [Employee]{
        return directory.getStaff().values.map { (item) -> Employee in
            return Employee(name: item.getName(), title: item.getRole())
        }
    }
    func createEmployess(filterColure:(Employee) -> Bool) -> [Employee]{
        return directory.getStaff().values.map({ (item) -> Employee in
            return Employee(name: item.getName(), title: item.getRole())
        })
    }
}
